import GameManager from "../Manager/GameManager";

const {ccclass, property} = cc._decorator;

@ccclass
export default class TipPanel extends cc.Component {
    public static Instance:TipPanel;

    // @property(cc.Label)
    // label: cc.Label = null;


    timer:number;
    timerCount:number;
    public list:string[];


    onLoad () {
        TipPanel.Instance = this;

        this.timerCount = this.timer = 0.6;
    }

    start () {
        this.list = [];
    }

    update (dt) {
        this.timerCount += dt;
        if (this.timerCount >= this.timer)
        {
            this.timerCount = 0;
            if (this.list.length > 0)
            {
                this.Show(this.list[0]);
                this.list.shift();
            }
        }
    }

    Open(content:string)
    {
        if (this.list.length == 0)
        {
            this.timerCount = this.timer;
        }
        this.list.push(content);
    }

    Show(content:string)
    {
        var child:cc.Node = this.GetFreeChild(this.node);
        child.active = true;
        child.setPosition(0, 0);
        var Text_current:cc.Label = child.getComponent(cc.Label);
        Text_current.string = content;

        child.runAction(cc.sequence(
            cc.fadeIn(0.2),
            cc.spawn(cc.sequence(cc.delayTime(2), cc.fadeOut(0.4)), cc.moveBy(2, 0, 300)),
            cc.callFunc(() => {
                child.active = false;
                // child.removeFromParent();
                // child.destroy();
            })
        ));

        // Tween tweener = child.DOMoveY(child.position.y + 200, 1.4f);
        // tweener.SetEase(Ease.Flash);
        // tweener.onComplete = () =>
        // {
        //     child.gameObject.SetActive(false);
        // };
    }

    GetFreeChild(parent:cc.Node):cc.Node
    {
        var c:cc.Node = null;
        for (var i = 0; i <= parent.childrenCount - 1; i++) {
            var child = parent.children[i];
            if(child.active == false)
            {
                c = child;
                break;
            }
        }

        if(c == null)
        {
            var node:cc.Node = cc.instantiate(parent.children[0]);
            node.parent = parent;
            node.setPosition(0, 0);
            c = node;
        }

        return c;
    }

}
